<template>
  <div class="home">
    <div class="box">
      <div class="tou">
        <img src="../assets/登录图片_03.jpg" alt="" />
      </div>
      <!-- 表单 -->
      <el-form :model="user" :rules="rules" ref="login" class="demo-ruleForm">
        <el-form-item prop="username">
          <el-input
            v-model="user.username"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="user.password"
            type="password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item class="up">
          <el-button type="primary" @click="login_user">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 表单 -->
    </div>
  </div>
</template>

<script>
import { loginApi } from '../http/api.js'
export default {
  data() {
    return {
      user: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 63到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {},
  methods: {
    // 登录
    login_user() {
      this.$refs['login'].validate(async (valid) => {
        if (valid) {
          const res = await loginApi(this.user)
          console.log(res)
          this.$router.push('/home')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 重置
    resetForm() {
      this.$refs['login'].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
  background: #2b4b6b;
  .box {
    width: 30%;
    height: 300px;
    background-color: #fff;
    box-shadow: 0 0 5px #fff;
    border-radius: 8px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-sizing: border-box;
    padding: 0 30px;
    min-width: 350px;
    .tou {
      position: absolute;
      top: -60px;
      left: 50%;
      transform: translateX(-50%);
      margin-bottom: 200px;
      img {
        width: 130px;
        height: 130px;
      }
    }
    .el-form {
      margin-top: 90px;
      .up {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
